home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / DatagramPacket.java < prev    next >
Text File  |  1998-09-22  |  5KB  |  170 lines

  1. /*
  2.  * @(#)DatagramPacket.java    1.13 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.net;
  16.  
  17. /**
  18.  * This class represents a datagram packet. 
  19.  * <p>
  20.  * Datagram packets are used to implement a connectionless packet 
  21.  * delivery service. Each message is routed from one machine to 
  22.  * another based solely on information contained within that packet. 
  23.  * Multiple packets sent from one machine to another might be routed 
  24.  * differently, and might arrive in any order. 
  25.  *
  26.  * @author  Pavani Diwanji
  27.  * @version 1.13, 07/01/98
  28.  * @since   JDK1.0
  29.  */
  30. public final 
  31. class DatagramPacket {
  32.     /*
  33.      * The fields of this class are package-private since DatagramSocketImpl 
  34.      * classes needs to access them.
  35.      */
  36.     byte[] buf;
  37.     int length;
  38.     InetAddress address;
  39.     int port;
  40.  
  41.     /**
  42.      * Constructs a <code>DatagramPacket</code> for receiving packets of 
  43.      * length <code>ilength</code>. 
  44.      * <p>
  45.      * The <code>length</code> argument must be less than or equal to 
  46.      * <code>ibuf.length</code>. 
  47.      *
  48.      * @param   ibuf      buffer for holding the incoming datagram.
  49.      * @param   ilength   the number of bytes to read.
  50.      * @since   JDK1.0
  51.      */
  52.     public DatagramPacket(byte ibuf[], int ilength) {
  53.     if (ilength > ibuf.length) {
  54.         throw new IllegalArgumentException("illegal length");
  55.     }
  56.     buf = ibuf;
  57.     length = ilength;
  58.     address = null;
  59.     port = -1;
  60.     }
  61.     
  62.     /**
  63.      * Constructs a datagram packet for sending packets of length 
  64.      * <code>ilength</code> to the specified port number on the specified 
  65.      * host. The <code>length</code> argument must be less than or equal 
  66.      * to <code>ibuf.length</code>. 
  67.      *
  68.      * @param   ibuf      the packet data.
  69.      * @param   ilength   the packet length.
  70.      * @param   iaddr     the destination address.
  71.      * @param   iport     the destination port number.
  72.      * @see     java.net.InetAddress
  73.      * @since   JDK1.0
  74.      */
  75.     public DatagramPacket(byte ibuf[], int ilength,
  76.               InetAddress iaddr, int iport) {
  77.     if (ilength > ibuf.length) {
  78.         throw new IllegalArgumentException("illegal length");
  79.     }
  80.     if (iport < 0 || iport > 0xFFFF) {
  81.         throw new IllegalArgumentException("Port out of range:"+ iport);
  82.     }
  83.     buf = ibuf;
  84.     length = ilength;
  85.     address = iaddr;
  86.     port = iport;
  87.     }
  88.     
  89.     /**
  90.      * Returns the IP address of the machine to which this datagram is being
  91.      * sent or from which the datagram was received.
  92.      *
  93.      * @return  the IP address of the machine to which this datagram is being
  94.      *          sent or from which the datagram was received.
  95.      * @see     java.net.InetAddress
  96.      * @since   JDK1.0
  97.      */
  98.     public synchronized InetAddress getAddress() {
  99.     return address;
  100.     }
  101.     
  102.     /**
  103.      * Returns the port number on the remote host to which this datagram is
  104.      * being sent or from which the datagram was received.
  105.      *
  106.      * @return  the port number on the remote host to which this datagram is
  107.      *          being sent or from which the datagram was received.
  108.      * @since   JDK1.0
  109.      */
  110.     public synchronized int getPort() {
  111.     return port;
  112.     }
  113.     
  114.     /**
  115.      * Returns the data received or the data to be sent.
  116.      *
  117.      * @return  the data received or the data to be sent.
  118.      * @since   JDK1.0
  119.      */
  120.     public synchronized byte[] getData() {
  121.     return buf;
  122.     }
  123.     
  124.     /**
  125.      * Returns the length of the data to be sent or the length of the
  126.      * data received.
  127.      *
  128.      * @return  the length of the data to be sent or the length of the
  129.      *          data received.
  130.      * @since   JDK1.0
  131.      */
  132.     public synchronized int getLength() {
  133.     return length;
  134.     }
  135.  
  136.     /**
  137.      * @since   JDK1.1
  138.      */
  139.     public synchronized void setAddress(InetAddress iaddr) {
  140.     address = iaddr;
  141.     }
  142.  
  143.     /**
  144.      * @since   JDK1.1
  145.      */
  146.     public synchronized void setPort(int iport) {
  147.     if (iport < 0 || iport > 0xFFFF) {
  148.         throw new IllegalArgumentException("Port out of range:"+ iport);
  149.     }
  150.     port = iport;
  151.     }
  152.  
  153.     /**
  154.      * @since   JDK1.1
  155.      */
  156.     public synchronized void setData(byte[] ibuf) {
  157.     buf = ibuf;
  158.     }
  159.  
  160.     /**
  161.      * @since   JDK1.1
  162.      */
  163.     public synchronized void setLength(int ilength) {
  164.     if (ilength > buf.length) {
  165.         throw new IllegalArgumentException("illegal length");
  166.     }
  167.     length = ilength;
  168.     }
  169. }
  170.